package role

import (
	"cgs-server/helper"
	"cgs-server/server"
	"cgs-server/server/base"
	"cgs-server/server/model"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"io/ioutil"
	"net/http"
)

func init() {
	//server.Handle(http.MethodPost, "/api/Role/Add", AddRole, server.Administrator)
	//server.Handle(http.MethodGet, "/api/Role/Get", GetRole, server.Administrator)
	//server.Handle(http.MethodGet, "/api/Role/List", ListRole, server.Administrator)
	//server.Handle(http.MethodPost, "/api/Role/Edit", EditRole, server.Administrator)
	//server.Handle(http.MethodPost, "/api/Role/Delete", DeleteRole, server.Administrator)

	server.Post("/api/Role/Add", AddRole, "添加角色", server.Login)
	server.Get("/api/Role/Get", GetRole, "获取角色", server.Login)
	server.Get("/api/Role/List", ListRole, "获取角色列表", server.Login)
	server.Post("/api/Role/Edit", EditRole, "更新角色", server.Login)
	server.Post("/api/Role/Delete", DeleteRole, "删除角色", server.Login)
	server.Post("/api/Role/UpdateUser", UpdateUser, "更新角色的用户", server.Login)
}

func AddRole(w http.ResponseWriter, r *http.Request) {
	r.Body = http.MaxBytesReader(w, r.Body, 100 << 20) // 这代表100M
	s, _ := ioutil.ReadAll(r.Body)

	role := &model.Role{}
	err := helper.FromJSON(s, role)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	if role.RoleName == "" || role.Type == "" {
		_, _ = helper.WriteJSON(w, server.Fail("RoleName and roleType is required"))
		return
	}

	roleService := NewRoleService()
	err = roleService.Init()
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	duModel := base.GenerateDataUserModel(role, r)
	err = roleService.Save(duModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	_, _ = helper.WriteJSON(w, server.OK("Add successfully!").WithData(bson.M{
		"roleId": role.Id,
	}))
}

// 更新角色的用户
func UpdateUser(w http.ResponseWriter, r *http.Request) {
	r.Body = http.MaxBytesReader(w, r.Body, 100 << 20) // 这代表100M
	s, _ := ioutil.ReadAll(r.Body)

	role := &model.Role{}
	err := helper.FromJSON(s, role)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	if role.Id.IsZero() || role.UserIds == nil {
		_, _ = helper.WriteJSON(w, server.Fail("RoleId and userIds is required"))
		return
	}

	roleService := NewRoleService()
	duModel := base.GenerateDataUserModel(role, r)
	err = roleService.UpdateUser(duModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	_, _ = helper.WriteJSON(w, server.OK("Update successfully!"))
}

// 根据 id 获取角色
func GetRole(w http.ResponseWriter, r *http.Request) {
	roleId, err := primitive.ObjectIDFromHex(r.FormValue("id"))
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	visualType := r.FormValue("visualType");
	if visualType == "" {
		visualType = "ThreeD"
	}

	roleService := NewRoleService()
	err = roleService.Init() // 此函数空参数的调用，使用在repository层初始化表名和CheckDB()来代替
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	duModel := base.GenerateDataUserModel(&model.Role{Id: roleId, VisualType: visualType}, r)

	result, err := roleService.GetByRoleId(duModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(result))
}

func ListRole(w http.ResponseWriter, r *http.Request) {
	// 之后会有分页的参数

	// 角色类型
	roleType := r.FormValue("type")
	// 应用id
	visualId := r.FormValue("visualId")
	//分类
	visualType := r.FormValue("visualType")
	if roleType == "" {
		_, _ = helper.WriteJSON(w, server.Fail("RoleType is required."))
		return
	}
	if roleType == model.AppRole && visualId == "" {
		_, _ = helper.WriteJSON(w, server.Fail("VisualId is required."))
		return
	}
	roleService := NewRoleService()
	err := roleService.Init()
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	duModel := base.GenerateDataUserModel(&model.Role{Type: roleType, VisualId: visualId, VisualType: visualType}, r)

	result, err := roleService.List(duModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(result))

}

func EditRole(w http.ResponseWriter, r *http.Request) {
	r.Body = http.MaxBytesReader(w, r.Body, 100 << 20) // 这代表100M
	s, _ := ioutil.ReadAll(r.Body)

	role := &model.Role{}
	err := helper.FromJSON(s, role)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	if role.RoleName == ""{
		_, _ = helper.WriteJSON(w, server.Fail("RoleName is required"))
		return
	}

	roleService := NewRoleService()
	err = roleService.Init()
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	duModel := base.GenerateDataUserModel(role, r)
	err = roleService.Update(duModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK("Edit successfully!"))
}

func DeleteRole(w http.ResponseWriter, r *http.Request) {
	roleId, err := primitive.ObjectIDFromHex(r.FormValue("id"))
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	role := &model.Role{
		Id: roleId,
	}
	duModel := base.GenerateDataUserModel(role, r)

	roleService := NewRoleService()
	err = roleService.Init()
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	// 角色为真实删除
	err = roleService.Remove(duModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK("Delete successfully!"))
}
